home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MCASM.RAR / MC_ASM.EXE / WROX_ASM / CH6 / PROG6_3.ASM < prev    next >
Assembly Source File  |  1994-09-11  |  10KB  |  391 lines

  1. ;****************************************
  2. ;*                    *
  3. ;*    Simple Screen Pencil Program    *
  4. ;*    Author: Yu.I.Petrenko        *
  5. ;*                    *
  6. ;****************************************
  7. .286
  8. cseg   segment BYTE
  9. ORG      100H
  10. assume    CS:cseg,DS:cseg,ES:cseg,SS:cseg
  11.           START:    JMP SET_UP
  12. ;=========================================================
  13. ;program data
  14.         Message1    DB    0Ah,0Dh
  15.     DB    'This simple Scren Pencil program is intended',0Ah,0Dh
  16.     DB    'for mouse control demonstration.',0Ah,0Dh
  17.     DB    'You can use following keys for:',0Ah,0Dh
  18.     DB    'SHIFT and mouse left button - to put pencil down,',0Ah,0Dh
  19.     DB    'TAB - to switch colors,',0Ah,0Dh
  20.     DB    'CapsLock - to trigger wipe UP/DOWN,',0Ah,0Dh
  21.     DB    'ESC and right mouse button - exit to DOS',0Ah,0Dh
  22.     DB    'Mouse moution and arrow keys cause light dot motion.',0Ah,0Dh
  23.     DB    'Best wishes from Author!',0Ah,0Dh
  24.     DB    'Please, strike any key ...',0Ah,0Dh,'$'
  25.         MSegm           DW      ?       ;memory segments
  26.         Beg_X           DW      ?       ;coordinate
  27.         Beg_Y           DW      ?       ;variables
  28.         Dot_X           DW      ?
  29.         Dot_Y           DW      ?
  30.         XOld            DW      ?
  31.         YOld            DW      ?
  32.         Color           DB      15      ;color variables
  33.         OldColor        DB      15
  34.         Fon        DB      0
  35.         Mode            DB      0       ;modes
  36.         OldVideo        DB      ?
  37.     OldPage         DB      ?
  38.         Inc_X           DW      0       ;coordinate
  39.         Inc_Y           DW      0       ;increments
  40.  
  41.         X1              DW      100     ;upper left
  42.         Y1              DW      15      ;corner
  43.         X2              DW      600     ;lower right
  44.         Y2          DW      315     ;corner
  45.     MStat        DW      ?       ;mouse status
  46.         KBStatus    DB      ?       ;keyboard status
  47.         Button_Number   DW      ?       ;mouse button number
  48.         Old_33h_off     DW      ?
  49.         Old_33h_SEG     DW      ?
  50.     Message2    DB    'Mouse driver not installed,'
  51.             DB    ' you',27h,'d better install it!',0Ah,0Dh
  52.             DB      'Please, strike any key ...','$'
  53. ;=========================================================
  54. SET_UP:    PUSH    CS
  55.     POP    DS
  56.  
  57.     MOV    AH,0Fh     ;storing old video parameters
  58.     INT     10h
  59.     MOV    OldVideo,AL
  60.     MOV    OldPage,BH
  61.  
  62.     MOV    AH,9       ; do message
  63.     LEA    DX,Message1;Type $-string
  64.     INT    21h
  65.     MOV    AX,0C01h
  66.     INT    21h       ;Wait for character input
  67. ;---------------------------------------------------------
  68. ; Checking, if the Mouse Driver is installed
  69.     MOV    AX,0
  70.         MOV     SI,7373h        ;set parole
  71.     INT    33h        ;Is our mouse driver installed?
  72.     CMP    AX,0
  73.     JNE    work        ;Yes
  74.  
  75.     MOV AH,9                ;Not installed,
  76.     LEA    DX,Message2    ;type message
  77.     INT    21h
  78.     MOV    AX,0C01h        ;Wait for character input
  79.     INT    21h
  80. ;---------------------------------------------------------
  81. work:   MOV     Button_Number,BX
  82.         MOV     AX,15h    ;Get necessary memory for mouse
  83.         MOV     SI,7373h  ;set parole
  84.     INT     33h
  85.     SHR     BX,4      ;Byte to paragraphs
  86.     MOV     AH,48h
  87.     INT     21h       ;Allocate memory, if needs
  88.         MOV     MSegm,AX;
  89.         MOV     ES,AX
  90.     XOR     DX,DX
  91.         MOV     AX,16h
  92.         MOV     SI,7373h        ;set parole
  93.         INT     33h             ;Save Mouse Driver State
  94. ;---------------------------------------------------------
  95.         MOV AX,MSegm    ; the buffer is used for to pass
  96.         MOV ES,AX       ; old Int 33h vector
  97.         MOV AX, ES:[0]
  98.         MOV Old_33h_off, AX
  99.         MOV AX, ES:[2]
  100.         MOV Old_33h_SEG, AX
  101.  
  102.     CALL    videoinit
  103.     MOV     Inc_X,16 ;repeat number
  104. ;---------------------------------------------------------
  105. r:    CALL    Rectangle ;frame creating
  106.     INC    X1
  107.     INC    Y1
  108.     DEC    X2
  109.     DEC    Y2
  110.     INC    color
  111.     DEC    Inc_X
  112.     JNZ    r
  113. ;---------------------------------------------------------
  114.     MOV    dot_X,240 ;put dot in the center
  115.     MOV    dot_Y,165
  116.     CALL    dot
  117. ;=========================================================
  118. Tick:
  119. ;---------------------------------------------------------
  120. ; In this big cicle all hot keys as well as a mouse
  121. ; are periodically requested. If any information
  122. ;received, it transformed into program state change
  123. ;or in the requested action or motion
  124. ;---------------------------------------------------------
  125. ;get motion control information from mouse motion
  126.     PUSH    CS
  127.     POP    DS
  128.     MOV    AX,3
  129.         MOV     SI,7373h        ;set parole
  130.     INT    33h             ;get mouse status
  131.     MOV    MStat,BX
  132.     TEST    MStat,2         ;is right button pressed?
  133.     JZ    motion
  134.     JMP    Exit
  135. motion:
  136.     MOV    AX,0Bh          ;get mouse replacement
  137.         MOV     SI,7373h        ;set parole
  138.     INT    33h
  139.     ADD    Inc_X,CX        ;light dot
  140.     ADD    Inc_Y,DX        ;motion
  141. ;---------------------------------------------------------
  142. ;get motion control information from keyboard
  143. KB:    MOV    KBStatus,0
  144.     MOV    AH,2
  145.     INT    16h
  146.     TEST    AX,40h
  147.     JZ    tt
  148.     MOV    KBStatus,1
  149. tt:    MOV    ah,0Bh
  150.     INT    21h
  151.     CMP    AL,0    ;buffer is empty
  152.     JZ    LSH
  153.     MOV    AH,0
  154.     INT    16h
  155.     CMP    AL,0
  156.     JZ    C_up    ;usual codes
  157.     CMP    AL,9    ;tabulation
  158.     JNZ    Out_
  159.     INC    color
  160.     AND    color,0fh
  161.     JMP    LSH
  162. Out_:    CMP    AL,27   ;exit, if ESC pressed
  163.     JNZ    LSH
  164.     JMP    Exit
  165. ; check for arrow codes
  166. C_up:    CMP    AH,72   ;control codes
  167.     JNZ    C_left
  168.     DEC    Inc_Y
  169.     DEC    Inc_Y
  170. C_left:    CMP    AH,75
  171.     JNZ    C_right
  172.     DEC    Inc_X
  173.     DEC    Inc_X
  174. C_right:
  175.     CMP    AH,77
  176.     JNZ    C_down
  177.     INC    Inc_X
  178.     INC    Inc_X
  179. C_down:    CMP    AH,80
  180.     JNZ    LSH
  181.     INC    Inc_Y
  182.     INC    Inc_Y
  183. ;---------------------------------------------------------
  184. ;check for mouse button and keyboard control
  185. LSH:    MOV    AH,2    ;check
  186.     INT    16h     ;if pressed?
  187.     TEST    MStat,1 ;mouse left button pressed?
  188.     JZ    tls
  189.     MOV    AL,2
  190. tls:    TEST    AL,2    ;Is Left Shift pressed?
  191.     JNZ    incr    ;Shift
  192.     CALL    Dot_Hide
  193. incr:    CALL    XY_Increment
  194.     CALL    Dot
  195.     JMP    Tick
  196. ;====================SUBROUTINES==========================
  197. videoinit:      ; video controller initialization
  198.     MOV    AH,0Fh
  199.     INT    10h
  200.     MOV    OldVideo,AL
  201.     MOV    AH,00      ;video setup
  202.     MOV    AL,10h
  203.     INT    10h
  204.     MOV    AH,11h  ;setup graphics
  205.     MOV    AL,22h  ;from ROM
  206.     MOV    BL,25
  207.     INT    10h
  208.     MOV    AH,6
  209.     MOV    AL,0
  210.     MOV    BH,fon
  211.     MOV    CX,0
  212.     MOV    DX,184Fh
  213.     INT    10h
  214.     RET
  215. ;=========================================================
  216. dot:
  217. ;this routine paints pixel
  218. ;DX=Y-coordinate;CX=X-coordinate; color = pixel color
  219.     MOV    BH,0              ;display page
  220.     MOV    CX,dot_X
  221.     MOV    DX,dot_Y
  222.     MOV    AH,0dh
  223.     INT    10h
  224.     MOV    BX,XOld
  225.     CMP    dot_X,BX
  226.     JZ    Y_comp
  227.     MOV    OldColor,AL
  228.     JMP    SetColor
  229. Y_comp:
  230.     MOV    BX,YOld
  231.     CMP    dot_Y,BX
  232.     JZ    SetColor
  233.     MOV    OldColor,AL
  234. SetColor:
  235.     MOV    BH,0            ;display page
  236.     MOV    AL,color        ;pixel color
  237.     MOV    CX,dot_X
  238.     MOV    XOld,CX
  239.     MOV    DX,dot_Y
  240.     MOV    YOld,DX
  241.     MOV    AH,0Ch
  242.     INT    10h
  243.     RET
  244. ;=========================================================
  245. Dot_Hide:
  246. ;this routine hides pixel
  247. ;DX=Y-coordinate;CX=X-coordinate; color = pixel color
  248.     MOV    BH,0              ;display page
  249.     TEST    KBStatus,1
  250.     JZ    wipe       ;fon_
  251.     MOV    AL,OldColor
  252. fff:    MOV    DX,dot_Y
  253.     MOV    CX,dot_X
  254.     MOV    AH,0Ch
  255.     INT    10h
  256.     RET
  257. wipe:   PUSH    dot_Y
  258.         mov     CX,6
  259.    w1:  MOV    AL,fon          ;set pixel color
  260.         PUSH    CX
  261.         call    fff
  262.         POP     CX
  263.         MOV    AX,Y2
  264.     CMP    Dot_Y,AX
  265.     JZ    lw1
  266.     INC    Dot_Y
  267.    lw1: LOOP    w1
  268.         POP     dot_Y
  269.         RET
  270. ;=========================================================
  271. Rectangle:
  272. ;this routine draws rectangle
  273.     MOV    mode,1  ;drow gorisontal line
  274.     MOV    AX,Y2
  275.     MOV    beg_Y,AX
  276.     MOV    AX,X1
  277.     MOV    beg_X,AX
  278.     MOV    DI,X2
  279.     CALL    _line
  280.     MOV    AX,Y1   ;the same
  281.     MOV    beg_Y,AX
  282.     MOV    AX,X1
  283.     MOV    beg_X,AX
  284.     MOV    DI,X2
  285.     CALL    _line
  286.     MOV    mode,0  ;drow vertical line
  287.     MOV    DI,Y2
  288.     CALL    _line
  289.     MOV    AX,X2   ; the same
  290.     MOV    beg_X,AX
  291.     MOV    DI,Y2
  292.     CALL    _line
  293.     RET
  294. ;=========================================================
  295. _line:
  296. ;drow the line    ;beg_Y=Y-coordinate
  297.                   ;beg_X=X-coordinate
  298.                   ;DI= line end
  299.                   ; color = that of dot,
  300.                   ;mode=0 - vertical line
  301.                   ;mode <> 0 - gorisontal line;
  302. ;---------------------------------------------------------
  303.     MOV    AX,beg_X
  304.     MOV    dot_X,AX
  305.     MOV    AX,beg_Y
  306.     MOV    dot_Y,AX
  307. a1:    CALL    dot        ;put the first dot
  308.  
  309.     CMP    mode,0     ;mode choice
  310.     JNZ    a2
  311.     JMP    a3
  312.  
  313. a2:    INC    dot_X      ;gorisontal line
  314.     CMP    dot_X,DI
  315.     JNZ    a1
  316.     RET
  317. a3:    INC    dot_Y
  318.     CMP    dot_Y,DI    ;vertical line
  319.     JNZ    a1
  320.     RET
  321. ;=========================================================
  322. XY_Increment:
  323. ;---------------------------------------------------------
  324. ; This routine supports coordinate incrementing along with
  325. ; the boundary limiting logic
  326. ;---------------------------------------------------------
  327. ; X-coordinate handling
  328.     XOR    AX,AX
  329.     CMP    AX,Inc_X
  330.     JZ    t2
  331.     JS    t1
  332.     INC    Inc_X
  333.     MOV    AX,X1
  334.     CMP    Dot_X,AX
  335.     JZ    t2
  336.     DEC    Dot_X
  337.     JMP    t2
  338. t1:    DEC    Inc_X
  339.     MOV    AX,X2
  340.     CMP    Dot_X,AX
  341.     JZ    t2
  342.     INC    Dot_X
  343. ; Y-coordinate handling
  344. t2:    XOR    AX,AX
  345.     CMP    AX,Inc_Y
  346.     JZ    t4
  347.     JS    t3
  348.     INC    Inc_Y
  349.     MOV    AX,Y1
  350.     CMP    Dot_Y,AX
  351.     JZ    t4
  352.     DEC    Dot_Y
  353.     JMP    t4
  354. t3:    DEC    Inc_Y
  355.     MOV    AX,Y2
  356.     CMP    Dot_Y,AX
  357.     JZ    t4
  358.     INC    Dot_Y
  359. t4:  RET
  360. ;=========================================================
  361. Exit:
  362. ; end routine actions
  363. ;---------------------------------------------------------
  364.     MOV    AH,00    ; video reset
  365.     MOV    AL,OldVideo  ;
  366.     INT    10h
  367.  
  368.     MOV    AH,6     ;Clear Screen
  369.     MOV    AL,0
  370.     MOV    BH,7
  371.     XOR    CX,CX
  372.     MOV    DX,2479h
  373.     INT    10h
  374.  
  375.         PUSH DS
  376.         PUSH Old_33h_off
  377.         PUSH Old_33h_SEG
  378.         POP DS
  379.         POP DX
  380.         MOV AX,MSegm
  381.         MOV ES,AX
  382.         MOV AH,49h
  383.         INT 21h      ;return allocated buffer
  384.  
  385.     MOV    AH,4Ch
  386.     INT    21h  ;program end
  387.  
  388. CSEG    ENDS
  389. END    START
  390. ;*********************************************************
  391.